package cn.com.haopy.yue.ai.wx.web;


import cn.com.haopy.yue.ai.core.utils.ResponseUtil;
import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskPapers;
import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskPapersOptions;
import cn.com.haopy.yue.ai.db.service.hos.AIRiskPapersOptionsService;
import cn.com.haopy.yue.ai.db.service.hos.AIRiskPapersService;
import cn.com.haopy.yue.ai.wx.annotation.LoginUser;
import cn.com.haopy.yue.ai.wx.dto.PapersAnswerDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 对象存储服务
 */
@RestController
@RequestMapping("/haopyYueAi/papersOptions")
@Api(tags = "问卷问题相关")
@Slf4j
public class WxRiskPaperOptionsController {

    @Autowired
    private AIRiskPapersService riskPapersService;


    @Autowired
    private AIRiskPapersOptionsService riskPapersOptionsService;


    @GetMapping("/infoById")
    @ApiOperation("问卷问题信息")
    public Object infoById(@LoginUser Integer userId,
                           @RequestParam @ApiParam("问卷ID") Integer paperId,
                           @RequestParam(required = false, defaultValue = "0") @ApiParam("序号") Integer sortIndex,
                           @RequestParam(required = false) @ApiParam("问题ID") Integer id) {
        if (Objects.isNull(userId)) {
            return ResponseUtil.unlogin();
        }
        AIRiskPapers papers = riskPapersService.getById(paperId);
        if (Objects.isNull(papers)) {
            return ResponseUtil.failMsg("问卷不存在");
        }

        return ResponseUtil.ok(riskPapersOptionsService.getOptionsById(paperId, id, sortIndex));
    }


    @PostMapping("/answerById")
    @ApiOperation("问卷问题信息-答题")
    public Object answerById(@LoginUser Integer userId, @RequestBody @Validated PapersAnswerDto answer) {
        if (Objects.isNull(userId)) {
            return ResponseUtil.unlogin();
        }
        AIRiskPapersOptions option = riskPapersOptionsService.getById(answer.getId());
        if (Objects.isNull(option)) {
            return ResponseUtil.failMsg("信息不存在");
        }

        option.setOptions(answer.getOptions());
        Boolean bool = riskPapersOptionsService.updateById(option);

        if (bool) {
            return ResponseUtil.ok();
        }

        return ResponseUtil.failMsg("保存失败！");


    }

}